var app = getApp()
Page({
  data: {
    // 顶部菜单切换
    navbar: ['全部', '待付款', "待发货", "待收货", "已完成"], //【空，0，1，2，3】    
    unpaidCount: 0, //待付款0
    unshippedCount: 0, //待发货1
    uncollectedCount: 0, //待收货2
    completeCount: 0, //已完成3
    // 默认选中菜单
    currentTab: 0,
    index: 0,
    pick_name: "",
    list: [],
    responseBody: {},
    dateList: {},
   
  },

  // 初始化加载
  onShow: function(e) {
    var that = this;

    //that.listdata()
  },
  // 初始化加载
  onLoad: function(e) {
    wx.showLoading({
      title: '正在加载中...',
    })
    var that = this;
    that.listdata()
  },
  //查询数据 个人订单
  listdata: function() {
    var that = this;
    wx.request({
      url: app.globalData.the_host + `/shopping/orders`,
      data: {
        "openid": app.globalData.openId
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        var data = res.data;
        var a = 0;
        var b = 0;
        var c = 0;
        for (var i = data.length - 1; i >= 0; i--) {
          if (data[i].expressStatus == 0) {
            a = a + 1;
          } else if (data[i].expressStatus == 1) {
            b = b + 1;
          } else if (data[i].expressStatus == 2) {
            c = c + 1;
          }
        }
        console.log(a, b, c)
        that.setData({          
          unpaidCount: a,
          unshippedCount: b,
          uncollectedCount: c,
          list: res.data,
        });
        console.log(that.data.unpaidCount, that.data.unshippedCount, that.data.uncollectedCount)
        wx.hideLoading(); //隐藏提示框
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },


  // 提交订单
  bindSubmitOrder: function(id) {
    
    var that = this;
    wx.request({
      url: app.globalData.the_host + '/order/wxpay?tenant=t1',
      data: {
        "orderId": id,
        "billCreateIp": app.globalData.ip //调用微信支付API的机器IP
      },
      method: 'post',
      header: {
        'content-type': 'application/json',
      },
      success: function(res) {
        if (res.data.status == 1) {         
          //提交失败
          wx.showToast({
            title: res.data.message,
            duration: 1000
          })
        } else {
          that.setData({
          
            responseBody: res.data.data,
          });
          that.zhifu(); //付款
        }

      },
    })
  },

  //支付
  zhifu: function(e) {
    var that = this;
    console.log(this.data.responseBody)
    //调起支付
    wx.requestPayment({
      'timeStamp': that.data.responseBody.timeStamp, // '1586710872629',
      'nonceStr': that.data.responseBody.nonce_str, //'gVih75fDfU7LHPqH',
      'package': that.data.responseBody.package, //'prepay_id=wx1301011262605543f11926071917582600',
      'signType': that.data.responseBody.signType, // 'MD5',
      'paySign': that.data.responseBody.paySign, //'A28BF0943A1557090D9856FEF5FA1977',
      'success': function(res) {
        console.log(res)
        that.updataOrder(1); //状态0待付款', "1待发货", "2待收货", "3已完成"
      },
      'fail': function(res) {
        that.updataOrder(0); //状态0待付款', "1待发货", "2待收货", "3已完成"
      },
      'complete': function(res) {
        console.log(res)
      }
    })
  },
  //确认支付
  qurenzhifu: function(e) {
    var that = this;
    that.setData({
      dateList: e.currentTarget.dataset.date,
    });
    console.log(that.data.dateList)
    wx.showModal({
      title: '提示',
      content: '确定提交付款？',
      success: function(res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          //点击确定
          that.bindSubmitOrder(that.data.dateList.id);
        }
      },
    })
  },
  bindtapShouHuo: function(e) {
    var that = this;
    that.setData({
      dateList: e.currentTarget.dataset.item,
    });
    that.updataOrder(3); //状态0待付款', "1待发货", "2待收货", "3已完成"
  },
  //确定支付时 修改当前订单状态
  updataOrder: function(expressStatus) {
    var that = this;

    wx.request({
      url: app.globalData.the_host + `/order/update?tenant=t1`,
      data: {
        "id": that.data.dateList.id, //修改id
        "openId": app.globalData.openId, //订单提交人
        "totalPrice": that.data.dateList.totalPrice, //总价
        "addressee": that.data.dateList.addressee, //收货人
        "addresseePhone": that.data.dateList.addresseePhone, //收货人号码
        "shipAddress": that.data.dateList.shipAddress, //收货地址
        "expressCompany": that.data.dateList.expressCompany, //快递公司
        "expressNum": that.data.dateList.expressNum, //快车号码
        "expressStatus": expressStatus, //状态0待付款', "1待发货", "2待收货", "3已完成"
        "note": that.data.dateList.note, //备注
        "salesChannel": "wx",
        "status": that.data.dateList.status,
        "orderDetail": that.data.dateList.orderDetail, //这个可以为空
      },
      method: 'PUT', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.status == 0) {
          //提交成功
          that.listdata();
        } else {
          //提交失败
          wx.showToast({
            title: `提交失败`,
            duration: 500
          })
        }

      },
    })

  },
  //顶部tab切换
  navbarTap: function(e) {
    //console.log(e.currentTarget.dataset.idx)
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  },
  /* 修改 */
  upOrder: function(e) {
    let obj = JSON.stringify(e.currentTarget.dataset.data)
    wx.navigateTo({
      url: '../uporder/index?obj=' + obj
    })
  }


})